﻿
Imports System.IO
Module NetStuff
    Public Function DownloadFile(ByVal httpPath As String, ByVal Destination As String) As Boolean
        Try
            Dim wr As System.Net.HttpWebRequest = CType(System.Net.WebRequest.Create(httpPath), System.Net.HttpWebRequest)
            Dim ws As System.Net.HttpWebResponse = CType(wr.GetResponse(), System.Net.HttpWebResponse)
            Dim str As Stream = ws.GetResponseStream()
            Dim inBuf(100000000) As Byte
            Dim bytesToRead As Long = CInt(inBuf.Length)
            Dim bytesRead As Long = 0
            While bytesToRead > 0
                Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead)
                If n = 0 Then
                    Exit While
                End If
                bytesRead += n
                bytesToRead -= n
            End While
            If File.Exists(Destination) Then
                File.Delete(Destination)
            End If
            Dim fstr As New FileStream(Destination, FileMode.Create, FileAccess.Write)
            fstr.Write(inBuf, 0, bytesRead)
            str.Close()
            fstr.Close()
            Return True
        Catch ex As Net.WebException
            LogStuff.ErrorLogWrite("No TU available", Form1.SelectedGameName)
            'MsgBox(ex.Message & vbCrLf & vbCrLf & httpPath)
            Return False

        End Try
    End Function
End Module